home *** CD-ROM | disk | FTP | other *** search
- program tststr3;
-
- {$APPTYPE CONSOLE}
-
- uses
- Windows,
- SysUtils;
-
- function PosCh(aCh : char; const S : string; aStart : integer) : integer;
- var
- i : integer;
- begin
- if aStart < 1 then
- aStart := 1;
- for i := aStart to length(S) do
- if (S[i] = aCh) then begin
- Result := i;
- Exit;
- end;
- Result := 0;
- end;
-
- function ConvertEscapes1(const aSt : string) : string;
- var
- i : integer;
- PosEscape: integer;
- ASCIIStr : string;
- ASCIIVal : integer;
- ec : integer;
- begin
- Result := aSt;
- i := 1;
- while i <= length(Result) do begin
- {find the next escape character in the remaining string}
- PosEscape := PosCh('\', Result, i);
- {if there is no escape, exit}
- if (PosEscape = 0) then
- Exit;
- {if this position is right at the end of the string, exit, we're
- done}
- if PosEscape = length(Result) then
- Exit;
- {if the next character is an backslash, then replace the double
- backslash by just one of them}
- if (Result[PosEscape+1] = '\') then
- Delete(Result, PosEscape, 1)
- {if the next character is a digit, there should be three of them,
- convert the four characters \nnn to an ASCII character, ignore
- all errors (ie, don't convert the backslash)}
- else if (Result[PosEscape+1] in ['0'..'9']) and
- (PosEscape <= length(Result) - 3) then begin
- ASCIIStr := Copy(Result, PosEscape+1, 3);
- Val(ASCIIStr, ASCIIVal, ec);
- if (ec = 0) and (ASCIIVal <= 255) then begin
- Delete(Result, PosEscape, 4);
- Insert(char(ASCIIVal), Result, PosEscape);
- end;
- end;
- i := PosEscape + 1;
- end;
- end;
-
- function ConvertEscapes(const aSt : string) : string;
- var
- DestInx : integer;
- SourceInx : integer;
- ASCIIStr : string;
- ASCIIVal : integer;
- ec : integer;
- begin
- {assume that we won't convert any escapes: the result string will be
- the same length as the source}
- SetLength(Result, length(aSt));
- {go through the source, character by character}
- DestInx := 0;
- SourceInx := 1;
- while SourceInx <= length(aSt) do begin
- {non-escape characters pass straight through}
- if (aSt[SourceInx] <> '\') then begin
- inc(DestInx);
- Result[DestInx] := aSt[SourceInx];
- inc(SourceInx);
- end
- {otherwise it's an escape character}
- else begin
- {if the escape is at the end of the source string, pass it
- through--there cannot be any other characters to convert}
- if (SourceInx = length(aSt)) then begin
- inc(DestInx);
- Result[DestInx] := '\';
- inc(SourceInx);
- end
- {if it's the first of a double escape, pass a single one
- through to the result string}
- else if (aSt[SourceInx+1] = '\') then begin
- inc(DestInx);
- Result[DestInx] := '\';
- inc(SourceInx, 2);
- end
- else if (aSt[SourceInx+1] in ['0'..'9']) and
- (SourceInx <= length(aSt) - 3) then begin
- ASCIIStr := Copy(aSt, SourceInx+1, 3);
- Val(ASCIIStr, ASCIIVal, ec);
- if (ec = 0) and (ASCIIVal <= 255) then begin
- inc(DestInx);
- Result[DestInx] := char(ASCIIVal);
- inc(SourceInx, 4);
- end;
- end
- {otherwise it *is* an escape character, but part of a badly
- formed sequence: just pass it through}
- else begin
- inc(DestInx);
- Result[DestInx] := '\';
- inc(SourceInx);
- end;
- end;
- end;
- {finally set the correct length of the result: DestInx is the index
- of the last character written}
- SetLength(Result, DestInx);
- end;
-
-
- var
- i : integer;
- StartTime : DWORD;
- begin
- writeln(ConvertEscapes1('abc\055'));
- writeln(ConvertEscapes1('abc\\\055'));
- writeln(ConvertEscapes1('abc\\\055\'));
- writeln(ConvertEscapes1('abc\\\055\a\056'));
- writeln(ConvertEscapes('abc\055'));
- writeln(ConvertEscapes('abc\\\055'));
- writeln(ConvertEscapes('abc\\\055\'));
- writeln(ConvertEscapes('abc\\\055\a\056'));
-
- writeln('testing normal routine...');
- StartTime := GetTickCount;
- for i := 1 to 100000 do
- ConvertEscapes1('\048\049\050\051\052\053\054\055\056\057');
- writeln('time taken: ', GetTickCount - StartTime);
-
- writeln('testing faster routine...');
- StartTime := GetTickCount;
- for i := 1 to 100000 do
- ConvertEscapes('\048\049\050\051\052\053\054\055\056\057');
- writeln('time taken: ', GetTickCount - StartTime);
- readln;
- end.
-